package com.d09cnpm2.group8.ar;

import java.io.IOException;

import com.d09cnpm2.group8.map.R;

import android.app.Activity;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.WindowManager;

public class CustomCamera extends Activity implements SurfaceHolder.Callback {
    private Camera cam;
    private SurfaceView camSurfaceView;
    private SurfaceHolder camSurfaceHolder;
    private OverlayView overlayView;
    

    @Override
    protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
		WindowManager.LayoutParams.FLAG_FULLSCREEN);
	setContentView(R.layout.cameramain);
	initCamera();
    }

    private void initCamera() {
	camSurfaceView = (SurfaceView) findViewById(R.id.surface_camera);
	camSurfaceHolder = camSurfaceView.getHolder();
	camSurfaceHolder.addCallback(this);
	cam = Camera.open();
	overlayView = (OverlayView) findViewById(R.id.surface_camera);
	overlayView.getHolder().setFormat(PixelFormat.TRANSLUCENT);
//	overlayView.set
    }

    @Override
    protected void onDestroy() {
	super.onDestroy();
	stopCamera();
    }

    private void stopCamera() {
	camSurfaceHolder.removeCallback(this);
	cam.stopPreview();
	cam.release();
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
	    int height) {
	startCamera(holder, width, height);

    }

    private void startCamera(SurfaceHolder holder, int width, int height) {
	Camera.Parameters p = cam.getParameters();
	for (Camera.Size s : p.getSupportedPreviewSizes()) {
	    p.setPreviewSize(s.width, s.height);
	    break;
	}

	cam.setParameters(p);
	try {
	    cam.setPreviewDisplay(holder);
	} catch (IOException e) {
	    e.printStackTrace();
	}
	cam.startPreview();
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {

    }
}
